<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="美食名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入美食名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="foodType">
        <el-select
          v-model="queryParams.foodType"
          placeholder="请选择美食类型"
          style="width: 200px"
          clearable
          @change="handleQuery"
        >
          <el-option
            :label="item.dictLabel"
            :value="item.dictValue"
            v-for="item in foodType"
            :key="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="图片" prop="picture">
        <el-input
          v-model="queryParams.picture"
          placeholder="请输入图片地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地区" prop="region">
        <el-input
          v-model="queryParams.region"
          placeholder="请输入地区"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入所在地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="票价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入票价"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详情" prop="detail">
        <el-input
          v-model="queryParams.detail"
          placeholder="请输入详情"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="点赞数" prop="likes">
        <el-input
          v-model="queryParams.likes"
          placeholder="请输入点赞数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['index:food:add']"
        >新增图片</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['index:food:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['index:food:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['index:food:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="foodList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="美食名称" align="center" prop="name" />
      <el-table-column label="类型" align="center" prop="foodType">
        <template v-slot="{ row }">
          {{ foodType.find((a) => a.dictValue == row.foodType).dictLabel }}
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="picture">
          <template v-slot="{ row }">
            <div
              class="a_b"
              v-if="row.picture && row.picture != ''"
              style="height: 100px; margin: 0 auto; width: 100px"
            >
              <img :src="baseUrl + row.picture" alt="" class="a_b_a" />
              <span class="a_b_b" style="text-align: center">
                <span
                  class="a_b_b_a"
                  @click="handlePictureCardPreview(row.picture)"
                >
                  <el-icon><zoom-in /></el-icon>
                </span>
              </span>
            </div>
          </template>
        </el-table-column>
      <el-table-column label="所在地址" align="center" prop="address" />
      <el-table-column label="票价" align="center" prop="price">
        <template v-slot="{ row }">
          {{ row.price + '元' }}
        </template>
      </el-table-column>
      <el-table-column label="详情" align="center" prop="detail" />
      <el-table-column label="点赞数" align="center" prop="likes" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['index:food:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['index:food:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改美食对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="foodRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="美食名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入美食名称" />
        </el-form-item>
        <el-form-item label="类型" prop="foodType">
          <el-select
            v-model="form.foodType"
            placeholder="请选择美食类型"
            clearable
          >
            <el-option
              :label="item.dictLabel"
              :value="item.dictValue"
              v-for="item in foodType"
              :key="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="picture">
          <div class="a">
              <div class="a_b" v-if="form.picture && form.picture != ''">
              <img :src="baseUrl + form.picture" alt="" class="a_b_a" />
              <span class="a_b_b">
                  <span
                  class="a_b_b_a"
                  @click="handlePictureCardPreview(form.picture)"
                  >
                  <el-icon><zoom-in /></el-icon>
                  </span>
                  <span
                  class="a_b_b_b"
                  @click="handleDeletePicture(form.picture, 4)"
                  >
                  <el-icon>
                      <Delete />
                  </el-icon>
                  </span>
              </span>
              </div>
              <div class="a_a" @click="uploadClick(4)" v-else>
              <el-icon>
                  <Plus />
              </el-icon>
              <input
                  ref="inputRef4"
                  name="file"
                  accept=".jpg,.jpeg,.png"
                  type="file"
                  style="display: none"
                  @change="(event) => changeFile(event, 4)"
              />
              </div>
          </div>
        </el-form-item>
        <el-form-item label="地区" prop="region">
          <el-input v-model="form.region" placeholder="请输入地区" />
        </el-form-item>
        <el-form-item label="所在地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入所在地址" />
        </el-form-item>
        <el-form-item label="票价" prop="price">
          <el-input v-model="form.price" placeholder="请输入票价" />
        </el-form-item>
        <el-form-item label="详情" prop="detail">
          <el-input v-model="form.detail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="点赞数" prop="likes">
          <el-input v-model="form.likes" placeholder="请输入点赞数" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
      v-model="dialogVisible"
      title="预览"
      width="800px"
      append-to-body
    >
      <img
        :src="dialogImageUrl"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
  </div>
</template>

<script setup name="Food">
import { listFood, getFood, delFood, addFood, updateFood } from "@/api/index/food";
import { uploadFile } from "@/api/imgupload/index";
import { getDicts } from '@/api/system/dict/data'

const { proxy } = getCurrentInstance();

const foodList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const foodType = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    foodType: null,
    picture: null,
    era: null,
    texture: null,
    region: null,
    address: null,
    price: null,
    detail: null,
    likes: null,
    valiFlag: null
  },
  rules: {
    name: [
      { required: true, message: "文物名称不能为空", trigger: "blur" }
    ],
    foodType: [
      { required: true, message: "类型不能为空", trigger: "change" }
    ],
    picture: [
      { required: true, message: "图片不能为空", trigger: "blur" }
    ],
    region: [
      { required: true, message: "地区不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "所在地址不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "票价不能为空", trigger: "blur" }
    ],
    detail: [
      { required: true, message: "详情不能为空", trigger: "blur" }
    ],
    likes: [
      { required: true, message: "点赞数不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    createBy: [
      { required: true, message: "创建人不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "修改时间不能为空", trigger: "blur" }
    ],
    updateBy: [
      { required: true, message: "修改人不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询美食列表 */
function getList() {
  loading.value = true;
  listFood(queryParams.value).then(response => {
    foodList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    foodType: null,
    picture: null,
    era: null,
    texture: null,
    region: null,
    address: null,
    price: null,
    detail: null,
    likes: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    valiFlag: null
  };
  proxy.resetForm("foodRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加美食";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFood(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改美食";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["foodRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFood(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFood(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除美食编号为"' + _ids + '"的数据项？').then(function() {
    return delFood(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('index/food/export', {
    ...queryParams.value
  }, `food_${new Date().getTime()}.xlsx`)
}

/** 图片上传 */
//图片上传
const inputRef4 = ref();
const dialogImageUrl = ref();
const dialogVisible = ref(false);
const baseUrl = import.meta.env.VITE_BASE_URL;
const uploadClick = (type) => {
  proxy.$refs[`inputRef${type}`].click();
};
const changeFile = async (e, type) => {
  let files = e.target.files;
  for (let i = 0; i < files.length; i++) {
    let _file = files[i];
    var file = new FormData();
    file.append("files", _file);
    let res = await uploadFile(file);
    if (res.code == 200) {
      if (type == 4) {
        form.value.picture = res.fileNames;
      }
    }
  }
};
const handlePictureCardPreview = (file) => {
  dialogImageUrl.value = baseUrl + file;
  dialogVisible.value = true;
};
const handleDeletePicture = (file, type) => {
  if (type == 4) {
      form.value.picture = "";
  }
};
function getFoodType() {
  getDicts("food_type").then((res) => {
    if (res.code == 200) {
      foodType.value = res.data;
    }
  });
}
getFoodType();
getList();
</script>
<style scoped lang="scss">
.tabclass {
  width: 100%;
  ::v-deep {
    .el-form-item__error {
      display: contents;
    }
  }
}
.content {
  position: relative;

  &::-webkit-scrollbar {
    width: 2px;
    /* 滚动条宽度 */
  }

  /* 自定义滚动条轨道 */
  &::-webkit-scrollbar-track {
    background: #f1f1f1;
    /* 轨道颜色 */
  }

  /* 自定义滚动条的滑块（thumb） */
  &::-webkit-scrollbar-thumb {
    background: #888;
    /* 滑块颜色 */
  }

  /* 当hover或active状态时，自定义滑块的颜色 */
  &::-webkit-scrollbar-thumb:hover {
    background: #555;
  }
}

.content_back {
  position: relative;

  &::after {
    content: "";
    position: absolute;
    // width: 100%;
    // height: 100%;
    // background: rgba(0,0,0,0.1);
    top: -20px;
    left: -20px;
    right: -20px;
    bottom: -20px;
    z-index: 100;
  }
}

.a {
  display: flex;
  flex-wrap: wrap;
}

.a_a {
  width: 104px;
  height: 104px;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
  display: flex;
  align-items: center;
  justify-content: center;

  &:hover {
    border-color: var(--el-color-primary);
  }
}

.a_b {
  margin: 0 8px 8px 0;
  border-radius: 6px;
  width: 104px;
  height: 104px;
  overflow: hidden;
  position: relative;
  border: 1px solid var(--el-border-color);

  .a_b_a {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .a_b_b {
    align-items: center;
    background-color: var(--el-overlay-color-lighter);
    color: #fff;
    cursor: default;
    display: inline-flex;
    font-size: 20px;
    height: 100%;
    justify-content: center;
    left: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    transition: opacity var(--el-transition-duration);
    width: 100%;

    .a_b_b_a {
      cursor: pointer;
      display: none;
    }

    .a_b_b_b {
      color: inherit;
      font-size: inherit;
      position: static;
      cursor: pointer;
      display: none;
      margin-left: 1rem;
    }
  }
}

.a_b:hover {
  .a_b_b {
    opacity: 1;
  }

  .a_b_b_a {
    display: block;
  }

  .a_b_b_b {
    display: block;
  }
}

.a_subTitle {
  font-size: 12px;
  color: red;
  line-height: 1.5;
  margin-top: 4px;
  width: 100%;
}

.b_subTitle {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
  line-height: 1.7;
  margin-top: 4px;
  width: 100%;

  p {
    margin: 0;
    padding: 0;
  }
}

.c {
  display: flex;
  align-items: center;
  margin: 4px 0;
  width: 100%;

  .c_a {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }

  .c_b {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }
}

.append ::v-deep(.el-input-group__append) {
  padding: 0 10px;
}

.d {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  width: 100%;

  .d_a {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }
}
</style>
